﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Follow.Common;
using Follow.Model;
using Follow.DataAccess;
using System.Transactions;

namespace Follow.Business
{
    public class MetaBO
    {
        public IList<Meta> findByDescricao(string descricao)
        {
            MetaDAO dao = new MetaDAO();
            return dao.findByName(descricao);
        }

        public IList<Meta> findAll()
        {
            MetaDAO dao = new MetaDAO();
            return dao.findAll();
        }

        public IList<Meta> save(Meta meta, Int32 idUsuario)
        {
            MetaDAO dao = new MetaDAO();
            IList<Meta> Metas = new List<Meta>();

            if (meta.IDMeta == Int32.MinValue || meta.IDMeta.Equals(0))
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                        Metas = dao.insert(meta, idUsuario);
                        scope.Complete();
                    }
                    catch
                    {
                        scope.Dispose();
                        throw;
                    }
                }

            }
            else
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                        Metas = dao.update(meta, idUsuario);

                        scope.Complete();
                    }
                    catch
                    {
                        scope.Dispose();
                        throw;
                    }
                }
            }

            return Metas;
        }

     
        public Meta load(Int32 id)
        {
            MetaDAO dao = new MetaDAO();
            return dao.findById(id);
        }

        public void delete(Int32 id, Int32 idUsuario)
        {
            MetaDAO dao = new MetaDAO();
            dao.delete(id, idUsuario);
        }

    }
}